package com.b2mining.integration.mail;
import java.util.*;
import java.util.*;import java.io.*;import javax.mail.*;import javax.mail.internet.*;import javax.activation.*;
//import javax.mail.*;
//import javax.mail.internet.*;

import org.apache.log4j.Logger;

import com.b2mining.integration.helper.Helper;
/**
 * Clase 
 * @since 27/07/2007
 * @author adminebiz <BR>
 * Analista Programador: Richard Linares
 */
public class Mailer {
	Logger log = Logger.getLogger(this.getClass());
	private static String MAILuserid;
    private static String MAILpassword;
	private static String MAILfrom        = null;			
	private static String MAILfromNombre  = null;			
	private static String MAILhost        = null;			
	private static String MAILhostname    = null;	
	private static Properties props;
   
	static {
		MAILfrom = Helper.mailFrom;
		MAILfromNombre = Helper.mailFromName;
		MAILhost = Helper.mailHost;
		MAILhostname = Helper.mailHostName;
		MAILuserid = Helper.mailUserId;
		MAILpassword = Helper.mailPassword;
		props = new Properties(); 
		props.put(MAILhostname, MAILhost); //mail.smtp.host,ip del servidor de correo
		props.put("mail.transport.protocol", "smtp");
		props.put("mail.smtp.auth", "true");
	}

	/**
	 * Metodo que envia un email.
	 * @param atts atributos para formar el email.
	 * @throws HelperException
	 */	
	public void sendMail(MailAttributes atts)  throws MessagingException
	{
		try{
	      	boolean debug = false;
		
			Session sesion = Session.getInstance(props,
					new MailAuthenticator(MAILuserid,MAILpassword));
	
			sesion.setDebug(debug);
			Message menssage = new MimeMessage(sesion);
			menssage.setFrom(new InternetAddress(MAILfrom,MAILfromNombre));
		    // Set the to address
			log.debug("utilizo ");
			
			
	    	InternetAddress[] recibientes = new InternetAddress[atts.getCountPara()];
	    	for(int i=0; i<recibientes.length;i++){
	    		Destinatario dest = atts.getDestinatario(i);
	    		log.debug("nro "+i+" nombre "+dest.getNombre()+" correo "+dest.getCorreo());
	    		recibientes[i] = new InternetAddress(dest.getCorreo(),dest.getNombre());
	    	}
	    	menssage.addRecipients(javax.mail.Message.RecipientType.TO,recibientes);
		   // Set the subject
		    menssage.setSubject(atts.getSubject());
		    // Set the date
		    menssage.setSentDate(new Date());
		    // Set the content
		    menssage.setText(atts.getMessage());
		    // Send message
		    //log.debug(""atts.getSubject()+atts.getMessage()+atts.getFrom());
		    
			log.info("inicia TRANSPORT");
		    Transport.send(menssage);
			log.info("fin de TRANSPORT");		
		}
		catch(Exception e){
			log.debug("error al enviar correo:"+e);
				throw new MessagingException(e.toString());
		}
	}
	
	public void sendMail2(MailAttributes atts,String file)  throws MessagingException
	{
		try{
	      	boolean debug = false;
		
			Session sesion = Session.getInstance(props,
					new MailAuthenticator(MAILuserid,MAILpassword));
	
			sesion.setDebug(debug);
			Message menssage = new MimeMessage(sesion);
			menssage.setFrom(new InternetAddress(MAILfrom,MAILfromNombre));
		    // Set the to address
			log.debug("utilizo ");
			
			
	    	InternetAddress[] recibientes = new InternetAddress[atts.getCountPara()];
	    	for(int i=0; i<recibientes.length;i++){
	    		Destinatario dest = atts.getDestinatario(i);
	    		log.debug("nro "+i+" nombre "+dest.getNombre()+" correo "+dest.getCorreo());
	    		recibientes[i] = new InternetAddress(dest.getCorreo(),dest.getNombre());
	    	}
	    	menssage.addRecipients(javax.mail.Message.RecipientType.TO,recibientes);
		   // Set the subject
		    menssage.setSubject(atts.getSubject());
		    // Set the date
		    menssage.setSentDate(new Date());
		   
		    // Set the content, comentado porke al enviar adjuntos no se usa
		    //menssage.setText(atts.getMessage());
		    
		    // Send message
		    //log.debug(""atts.getSubject()+atts.getMessage()+atts.getFrom());
		    
		    log.info("Existe archivo adjunto, mensaje multipart "+file);
            //primera parte del mensaje, adjunto el texto del mail
            MimeBodyPart messageBodyPart = new MimeBodyPart();
            //fill message
            messageBodyPart.setText(atts.getMessage());
            Multipart multipart = new MimeMultipart();
            multipart.addBodyPart(messageBodyPart);
            //segunda parte del mensaje, adjunto el archivo
            messageBodyPart = new MimeBodyPart();
            DataSource source = new FileDataSource(file);
            messageBodyPart.setDataHandler(new DataHandler(source));
            messageBodyPart.setFileName((new File(file)).getName());
            multipart.addBodyPart(messageBodyPart);
            menssage.setContent(multipart);
		    
			log.info("inicia TRANSPORT");
		    Transport.send(menssage);
			log.info("fin de TRANSPORT");		
		}
		catch(Exception e){
			log.debug("error al enviar correo:"+e);
				throw new MessagingException(e.toString());
		}
	}
	

}
